package com.wsm.common.service.impl;

import com.wsm.common.mapper.ShiroPermissionDao;
import com.wsm.common.domain.ShiroPermissionDO;
import com.wsm.common.service.ShiroPermissionService;
import com.wsm.common.utils.SnowFlake;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.mgt.DefaultFilterChainManager;
import org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver;
import org.apache.shiro.web.servlet.AbstractShiroFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


@Service
public class ShiroPermissionServiceImpl implements ShiroPermissionService {
	@Autowired
	private ShiroPermissionDao shiroPermissionDao;
	@Autowired
	ShiroFilterFactoryBean shiroFilterFactoryBean;

	@Override
	public ShiroPermissionDO get(String id) {
		return shiroPermissionDao.get(id);
	}

	@Override
	public List<ShiroPermissionDO> list(Map<String, Object> map) {
		return shiroPermissionDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return shiroPermissionDao.count(map);
	}
	
	@Override
	public int save(ShiroPermissionDO shiroPermission){
		shiroPermission.setId(SnowFlake.nextId());
		return shiroPermissionDao.insert(shiroPermission);
	}
	
	@Override
	public int update(ShiroPermissionDO shiroPermission){
		return shiroPermissionDao.orUpdate(shiroPermission);
	}

	@Override
	public int remove(String id) {
		return shiroPermissionDao.remove(id);
	}

	@Override
	public int batchRemove(String[] ids) {
		return shiroPermissionDao.batchRemove(ids);
	}


	/**
	 * 重新加载权限
	 */
	public void updatePermission() {
		synchronized (shiroFilterFactoryBean) {
			Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
			List<ShiroPermissionDO> list = shiroPermissionDao.list(null);
			for (ShiroPermissionDO shiroPermissionDO : list) {
				filterChainDefinitionMap.put(shiroPermissionDO.getUrl(),
						shiroPermissionDO.getPermission());
			}

			AbstractShiroFilter shiroFilter = null;
			try {
				shiroFilter = (AbstractShiroFilter) shiroFilterFactoryBean
						.getObject();
			} catch (Exception e) {
				throw new RuntimeException(
						"get ShiroFilter from shiroFilterFactoryBean error!");
			}
			PathMatchingFilterChainResolver filterChainResolver = (PathMatchingFilterChainResolver) shiroFilter
					.getFilterChainResolver();
			DefaultFilterChainManager manager = (DefaultFilterChainManager) filterChainResolver
					.getFilterChainManager();
			// 清空老的权限控制
			manager.getFilterChains().clear();

			shiroFilterFactoryBean.getFilterChainDefinitionMap().clear();
			shiroFilterFactoryBean
					.setFilterChainDefinitionMap(filterChainDefinitionMap);
			// 重新构建生成
			Map<String, String> chains = shiroFilterFactoryBean
					.getFilterChainDefinitionMap();
			for (Map.Entry<String, String> entry : chains.entrySet()) {
				String url = entry.getKey();
				String chainDefinition = entry.getValue().trim()
						.replace(" ", "");
				manager.createChain(url, chainDefinition);
			}
			System.out.println("更新权限成功！！");
		}
	}
}
